package com.ecarx.module_udisk.recoveryos;

import android.util.Log;

import com.ecarx.library_base.scoket.SocketClient;
import com.ecarx.library_base.scoket.SocketConfig;
import com.ecarx.library_base.scoket.model.SocketResponse;

import org.json.JSONException;
import org.json.JSONObject;

public class RecoveryTrigger {

    private static final String TAG = "RecoveryTrigger";

    /**
     * 收到的返回的 code 信息
     */
    public static final int CODE_LUNCH_MSG_RECEIVED = 0;
    public static final int CODE_LUNCH_RECOVER_SUCCESS = 1;
    public static final int CODE_LUNCH_RECOVER_FAIL = -1;

    private SocketClient mSocketClient;

    private IRecoveryCallback mRecoveryCallback;

    private RecoveryTrigger() {

    }

    private static class Inner {
        private static final RecoveryTrigger sRecoveryTrigger = new RecoveryTrigger();
    }

    public static RecoveryTrigger getInstance() {
        return Inner.sRecoveryTrigger;
    }

    /**
     * 初始化 Socket
     */
    private void initSocket() {
        if (mSocketClient == null) {
            mSocketClient = new SocketClient(SocketConfig.DOM0.HOST, SocketConfig.DOM0.PORT, new SocketClient.SocketCallback() {
                @Override
                public void onConnected() {
                    sendLunchMessage();
                }

                @Override
                public void onSocketCallback(SocketResponse response) {
                    Log.d(TAG, "onSocketCallback() called with: response = [" + response + "]");
                    parseMessage(response);
                }

                @Override
                public void onError() {
                    if (mRecoveryCallback != null) {
                        mRecoveryCallback.onTrigger(false);
                    }
                }
            });
            mSocketClient.start();
        }
    }

    /**
     * 解析接收到的信息
     * @param response
     */
    private void parseMessage(SocketResponse response) {
        Log.d(TAG, "parseMessage() called with: response = [" + response + "]");
        if (mRecoveryCallback == null) {
            return;
        }
        switch (response.code) {
            case CODE_LUNCH_MSG_RECEIVED:
                mRecoveryCallback.onRecoveryGetMessage();
                break;
            case CODE_LUNCH_RECOVER_SUCCESS:
                mRecoveryCallback.onTrigger(true);
                break;
            case CODE_LUNCH_RECOVER_FAIL:
                mRecoveryCallback.onTrigger(false);
                break;
            default:
                Log.e(TAG, "Error parseMessage: " + response);
                break;
        }
    }

    /**
     * 发送信息给 Recovery OS
     * @param payload
     */
    public void sendMessage(String payload) {
        Log.d(TAG, "sendMessage() called with: payload = [" + payload + "]");
        if (mSocketClient != null) {
            mSocketClient.sendMsg(payload);
        }
    }

    /**
     * 设置监听回调
     * @param recoveryCallback
     * @return
     */
    public RecoveryTrigger setCallback(IRecoveryCallback recoveryCallback){
        mRecoveryCallback = recoveryCallback;
        return this;
    }

    /**
     * 准备启动 Recovery OS
     */
    public void lunch() {
        if (mSocketClient == null) {
            initSocket();
        } else {
            sendLunchMessage();
        }
    }

    /**
     * 发送启动 RecoveryOS 的信息
     */
    private void sendLunchMessage(){
        try {
            String lunchMessage = getLunchMessage();
            sendMessage(lunchMessage);
        } catch (JSONException e) {
            throw new RuntimeException(e);
        }
    }

    /**
     * 构造启动的 Message 信息
     * @return
     */
    private String getLunchMessage() throws JSONException {
        JSONObject jsonObject = new JSONObject();
        jsonObject.put("msgId", 3001);
        jsonObject.put("seq", System.currentTimeMillis());
        jsonObject.put("version", "0");
        return jsonObject.toString();
    }

    /**
     * 释放资源
     */
    public void release(){
        if (mSocketClient != null) {
            mSocketClient.stop();
        }
        mRecoveryCallback = null;
    }
}
